<template>
  <!-- 登录表单 -->
  <div class="loginFormContainer">
    <el-form
      ref="loginForm"
      :model="loginForm"
      label-width="70px"
      :rules="loginFormRules"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="手机号" prop="phoneNumber">
            <el-input v-model="loginForm.phoneNumber" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="loginForm.password"
              clearable
              show-password
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" justify="center">
        <el-col :span="20">
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" justify="end">
        <el-col :span="10">
          <span>没有账号？<router-link to="/registerAndLogin/register">去注册</router-link></span>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        phoneNumber: "",
        password: "",
      },
      loginFormRules: {
        phoneNumber: [
          { required: true, message: "请输入手机号", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            await this.$store.dispatch("login", this.loginForm);
            this.$router.replace({
              name: "home",
            });
          } catch (error) {
            alert(error);
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
.loginFormContainer {
  display: flex;
  justify-content: center;
  .el-form .el-row:not(:first-child){
    margin-top: 10px;
  }
  .el-button{
    width: 120px;
  }
  span{
    font-size: 14px;
    text-align: center;
    a{
      color: plum;
      text-decoration: none;
    }
  }
}
</style>